package com.networkmmc.demo.sql;

import java.util.Calendar;

/**
 * SQL语句中的日期处理
 */
public class SqlDtUtils {

    public static Calendar getCalendar(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.MONTH, 1);
        return cal;
    }

    /**
     * 每天毫秒数
     */
    public static final long DAY = 24 * 60 * 60 * 1000;

    public static long day(Calendar calendar) {
        Calendar cal = getCalendar();

        cal.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        return cal.getTimeInMillis();
    }

    public static long nextDay(Calendar calendar) {
        Calendar cal = getCalendar();

        cal.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
        cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        return cal.getTimeInMillis();
    }

    public static long month(Calendar calendar) {
        Calendar cal = getCalendar();

        cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        return cal.getTimeInMillis();
    }

    public static long nextMonth(Calendar calendar) {
        Calendar cal = getCalendar();

        cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
        cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        return cal.getTimeInMillis();
    }

    public static long year(Calendar calendar) {
        Calendar cal = getCalendar();

        cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        return cal.getTimeInMillis();
    }

    public static long nextYear(Calendar calendar) {
        Calendar cal = getCalendar();

        cal.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
        return cal.getTimeInMillis();
    }
}
